package com.cskaoyan.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

public class BatisUtils {

    public static SqlSession getSqlSession() {
        // 1. 获取一个SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 2. 获取一个SqlSessionFactory
        SqlSessionFactory build = null;
        try {
            build = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 3. 获取SqlSession
        SqlSession sqlSession = build.openSession(true);
        return sqlSession;
    }
}
